﻿using AE_BEPUPhysics_Addition;
using TEngine;
using UnityEngine;

namespace GameLogic
{
    public class ShipView : MonoBehaviour
    {
        // AEMeshVolumnBaseCollider m_Collider;
        SpriteRenderer m_SpriteRenderer;
        public SpriteRenderer SpriteRenderer { get => m_SpriteRenderer; set => m_SpriteRenderer = value; }
        private void Awake()
        {
            // m_Collider = transform.FindChildComponent<AEMeshVolumnBaseCollider>("Root/LogicBody");
            m_SpriteRenderer = transform.FindChildComponent< SpriteRenderer > ("Root/ShipSprite");
        }

        public void Update()
        {
            //m_SpriteRenderer.transform.position = Vector3.Lerp(m_SpriteRenderer.transform.position, m_CurFramePos, .5f);
            //m_SpriteRenderer.transform.rotation = Quaternion.Lerp(m_SpriteRenderer.transform.rotation, m_CurFrameRot, .5f);
            //m_LogicFrameTimeCount += Time.deltaTime;
            //float logicFrameTimeProgress = Mathf.Clamp(m_LogicFrameTimeCount / SystemModule.Instance.m_LogicDelta.ToFloat(), 0, 1f);
            //m_SpriteRenderer.transform.position = Vector3.Lerp(m_PreviousFramePos, m_CurFramePos, logicFrameTimeProgress);
            //m_SpriteRenderer.transform.rotation = Quaternion.Lerp(m_PreviousFrameRot, m_CurFrameRot, logicFrameTimeProgress);

        }
        public void Clean()
        {

        }

        public void UpdatePosition(Vector3 pos)
        {
            m_SpriteRenderer.transform.position = pos;
        }
        public void UpdateRotation(Quaternion pos)
        {
            m_SpriteRenderer.transform.rotation = pos;
        }
        public void Init(ShipDataContainer shipData)
        {
            m_SpriteRenderer.sprite = GameModule.Resource.LoadAsset<Sprite>(shipData.SpritePath);
        }

        public Vector2Int GetResulution()
        {
            return new UnityEngine.Vector2Int(m_SpriteRenderer.sprite.texture.width, m_SpriteRenderer.sprite.texture.height);
        }
        float m_LogicFrameTimeCount = 0;
        Vector3 m_PreviousFramePos;
        Quaternion m_PreviousFrameRot = Quaternion.identity;
        Vector3 m_PrevisouFrameScale;
        Vector3 m_CurFramePos;
        Quaternion m_CurFrameRot = Quaternion.identity;
        Vector3 m_CurFrameScale;


        public void UpdateLogicFrame(Vector3 pos, Quaternion rot, Vector3 scale)
        {
            m_LogicFrameTimeCount = 0;
            m_PreviousFramePos = m_CurFramePos;
            m_PreviousFrameRot = m_CurFrameRot;
            m_PrevisouFrameScale = m_CurFrameScale;

            m_CurFramePos = pos;
            m_CurFrameRot = rot;
            m_CurFrameScale = scale;
        }
        public void SetPosition(BEPUutilities.Vector2 pos)
        {
            UpdateLogicFrame(new Vector3(pos.X.ToFloat(), pos.Y.ToFloat(), 0), m_CurFrameRot, m_CurFrameScale);
        }
        public void SetOrientation(BEPUutilities.Quaternion quat)
        {
            UpdateLogicFrame(m_CurFramePos, quat.ToFloat(), m_CurFrameScale);
        }
    }
}
